package com.johnny.cms;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.tools.ant.taskdefs.TempFile;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.johnny.cms.core.domain.menu.Menu;
import com.johnny.cms.core.domain.menu.MenuGroup;
import com.johnny.cms.core.domain.template.Template;
import com.johnny.cms.core.manager.trans.IContentManager;

/**
 * <p>
 * InitData.java
 * <li>Introduce</li> First draft at 16 Sep 2011
 * 
 * @author <a href="mailto:johnnywalee@gmail.com">Johnny</a>
 */
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = false, transactionManager = "simTransactionManager")
@Transactional()
@ContextConfiguration(locations = { "classpath:application-root.xml" })
public class InitData {
	@Autowired
	private IContentManager contentManager;

	@BeforeClass
	public static void init() throws SQLException, IOException {
		try {
			Db.main(null);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Test
	public void initMenu() {
		MenuGroup menuGroup = new MenuGroup();
		menuGroup.setName("header");
		menuGroup.setDescription("For title");
		contentManager.saveMenuGroup(menuGroup);

		Menu whoWeAre = getMenu(menuGroup, "Who we are", 0, "index", null);
		getMenu(menuGroup, "Our Philosophy", 0, "who-philosophy", whoWeAre);
		getMenu(menuGroup, "NBO Team", 1, "who-team", whoWeAre);

		Menu ourSolutions = getMenu(menuGroup, "Our Philosophy", 1,
				"solutions", null);
		getMenu(menuGroup, "Overview", 0, "solutions-solutions", ourSolutions);
		getMenu(menuGroup, "Consulting", 1, "solutions-consulting",
				ourSolutions);
		getMenu(menuGroup, "Leadership", 2, "solutions-leadership",
				ourSolutions);
		getMenu(menuGroup, "Management Effectiveness", 3,
				"solutions-management", ourSolutions);
		getMenu(menuGroup, "Team Development", 4, "solutions-team",
				ourSolutions);
		getMenu(menuGroup, "Communication Effectiveness", 5,
				"solutions-communication", ourSolutions);
		getMenu(menuGroup, "360 Feedback System", 6, "solutions-360feedback",
				ourSolutions);
		getMenu(menuGroup, "What Bosses Want", 7, "solutions-boss",
				ourSolutions);

		Menu clientSuccesses = getMenu(menuGroup, "Client Successes", 2,
				"client", null);
		getMenu(menuGroup, "Clients", 0, "client-list", clientSuccesses);
		getMenu(menuGroup, "Case Studies", 1, "client-case", clientSuccesses);

		Menu partner = getMenu(menuGroup, "Partners", 3, "partner", null);

		getMenu(menuGroup, "Find Our Partners", 0, "partner-map", partner);
		getMenu(menuGroup, "Join Our Alliance", 1, "join", partner);

		Menu resource = getMenu(menuGroup, "Resources", 4, "resource", null);

		getMenu(menuGroup, "Brochures", 0, "resource-brochures", resource);
		getMenu(menuGroup, "Articles", 1, "resource-articles", resource);
		getMenu(menuGroup, "Whitepapers", 2, "resource-whitepapers", resource);
		getMenu(menuGroup, "Videos", 3, "resource-videos", resource);

	}

	@Test
	public void initSecond() {
		MenuGroup menuGroup = new MenuGroup();
		menuGroup.setName("second");
		menuGroup.setDescription("second");
		contentManager.saveMenuGroup(menuGroup);

		getMenu(menuGroup, "Blog", 0, "blog", null);
		getMenu(menuGroup, "Gallery", 0, "gallery", null);
		getMenu(menuGroup, "Contact Us", 0, "contact", null);

	}

	@Test
	public void template() {
		disableInline("nbo/blog-article");
		disableInline("nbo/blog");
		disableInline("master");
		disableInline("nbo/search");
		disableInline("nbo/download");
		disableInline("nbo/gallery-album43a0");
		disableInline("nbo/gallery");
		disableInline("nbo/resource-article");
	}

	public void disableInline(String name) {
		Template template = contentManager.getTemplateByName(name);
		if (template == null) {
			System.out.println("fail to get template by name " + name);
		} else {
			template.setInlineEdit(false);
			contentManager.saveTemplateNoFileSave(template);
		}
	}

	private Menu getMenu(MenuGroup menuGroup, String title, int order,
			String channel, Menu father) {
		Menu menu = new Menu();
		menu.setMenuGroup(menuGroup);
		menu.setName(title);
		menu.setTitle(title);
		menu.setRankOrder(order);
		menu.setChannel(contentManager.getChannelByPath(channel));
		menu.setFather(father);
		contentManager.saveMenu(menu);
		return menu;
	}

	@AfterClass
	public static void finish() {
		System.out.println("finish init db data");
	}

}
